﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EnvironmentalMonitor.Support.Hibernate.Manage;
using EnvironmentalMonitor.Support.Hibernate;
using EnvironmentalMonitor.Support.Module.Manage;
using EnvironmentalMonitor.Support.Module;

namespace EnvironmentalMonitor.Support.Business.Manage
{
    /// <summary>
    /// 岗位和页面关系业务类
    /// </summary>
    public class PostPageBusiness
    {
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="rows">每页行数</param>
        /// <param name="total">总数</param>
        /// <returns>岗位和页面关系集合</returns>
        public List<PostPage> Query(int page, int rows, ref int total)
        {
            List<PostPage> results = new List<PostPage>();

            PostPageHibernate hibernate = new PostPageHibernate();
            results = hibernate.Query(page, rows, ref total);

            return results;
        }

        /// <summary>
        /// 通过岗位查询
        /// </summary>
        /// <param name="post">岗位</param>
        /// <param name="total">总数</param>
        /// <returns>岗位和页面关系集合</returns>
        public List<PostPage> QueryByPost(Post post, ref int total)
        {
            List<PostPage> results = new List<PostPage>();

            PostHibernate postHibernate = new PostHibernate();
            post = postHibernate.QueryByGuid(post.Guid);

            PageHibernate pageHibernate = new PageHibernate();
            List<Page> pages = pageHibernate.Query(1, int.MaxValue, ref total);

            PostPageHibernate postPageHibernate = new PostPageHibernate();
            List<PostPage> postPages = postPageHibernate.QueryByPost(post.Guid);

            for (int i = 0; i < pages.Count; i++)
            {
                PostPage postPage = new PostPage();
                postPage.PostId = post.Guid;
                postPage.PageId = pages[i].Guid;
                postPage.PostName = post.Name;
                postPage.PageName = pages[i].Name;
                postPage.PageRemark = pages[i].Remark;

                for (int j = 0; j < postPages.Count; j++)
                {
                    if (pages[i].Guid == postPages[j].PageId)
                    {
                        postPage = postPages[j];
                        break;
                    }
                }
                results.Add(postPage);
            }
            total = results.Count;

            return results;
        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="value">值</param>
        /// <returns>结果</returns>
        public bool Insert(PostPage value)
        {
            bool result = false;

            PostPageHibernate hibernate = new PostPageHibernate();
            result = hibernate.Insert(value);

            return result;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="guid">主键</param>
        /// <returns>结果</returns>
        public bool Delete(string guid)
        {
            bool result = false;

            PostPageHibernate hibernate = new PostPageHibernate();
            result = hibernate.Delete(guid);

            return result;
        }
    }
}
